package com.market.cash.ui.activity

import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.market.cash.R
import com.market.cash.databinding.CashPermissionBinding
import com.market.cash.databinding.CashPermissionIndicatorBinding
import com.market.cash.pro.ProActivity
import com.market.cash.pro.ProFragment
import com.market.cash.ui.fragment.PermissionFragment
import com.market.cash.ui.presenter.CashRootPermissionPresenter
import com.market.cash.ui.view.ICashRootPermissionView
import com.market.cash.utils.CashRootBarUtil
import com.market.cash.utils.event.CashRootEventType
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/17 14:04
 * @Annotation：权限
 */
class PermissionActivity: ProActivity(),ICashRootPermissionView {
    lateinit var binding: CashPermissionBinding

    var presenter: CashRootPermissionPresenter? = null
    private var pageList: ArrayList<ProFragment>? = null

    override fun getCashRootRootView(): View? {
        binding = CashPermissionBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCashRootView() {
        CashRootBarUtil.setStatusBarLightMode(this, false)
        pageList = arrayListOf()
    }

    override fun initCashRootListener() {
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                presenter!!.onCashRootCheckPageClick(position)
            }
        })

        binding.checkBox.setOnCheckedChangeListener { _, isChecked ->
            when (isChecked) {
                true -> {
                    binding.btnAgree.setBackgroundResource(R.drawable.cash_radius_fill_435cfa_12)
                    binding.btnAgree.isEnabled = true
                }
                false -> {
                    binding.btnAgree.setBackgroundResource(R.drawable.cash_radius_fill_black_alph40_12)
                    binding.btnAgree.isEnabled = false
                }
            }
        }
        //拒绝
        binding.btnDeny.setOnClickListener {
            presenter!!.onCashRootDenyClick()
        }

        //同意权限
        binding.btnAgree.setOnClickListener {
            presenter!!.onCashRootAgreeClick()
        }

        //已授权
        binding.authorizedLayout.setOnClickListener{
        }

        //被禁止
        binding.bannedLayout.setOnClickListener {
            presenter!!.onCashRootBannedClick()
        }
    }

    override fun initCashRootData() {
        presenter = CashRootPermissionPresenter(this)
        presenter!!.loadCashRootPermissionListAction()
    }

    override fun onResume() {
        super.onResume()
        presenter!!.onCashRootPermissionResume()
    }

    /**
     * 设置权限列表
     */
    override fun showPermissionList(pList: ArrayList<JSONObject>) {
        pList.forEach {
            pageList!!.add(PermissionFragment(it))
        }
        val pageAdapter = MyFragmentPagerAdapter(this,pageList!!)
        binding.viewPager.adapter = pageAdapter
    }

    /**
     * 设置显示权限
     */
    override fun showPermissionPage(index: Int) {
        binding.viewPager.currentItem = index
        addIndicatorViews(index)

        val item = presenter!!.getCashRootPermissionList()[index]
        binding.imgPermission.setImageResource(item.optInt("icon"))
        binding.textTitle.text = item.optString("title")
        binding.textTitle.setTextColor(item.optInt("titleColor"))
    }

    /**
     * 未申请
     */
    override fun showPermissionNotObtained() {
        binding.defaultLayout.visibility = View.VISIBLE
        binding.authorizedLayout.visibility = View.GONE
        binding.bannedLayout.visibility = View.GONE

        binding.readTipsLayout.visibility = View.VISIBLE

        binding.checkBox.isChecked = true
        binding.btnAgree.setBackgroundResource(R.drawable.cash_radius_fill_435cfa_12)
        binding.btnAgree.isEnabled = true
    }

    /**
     * 已允许
     */
    override fun showPermissionAuthorized() {
        binding.defaultLayout.visibility = View.GONE
        binding.authorizedLayout.visibility = View.VISIBLE
        binding.bannedLayout.visibility = View.GONE

        binding.readTipsLayout.visibility = View.GONE
    }

    /**
     * 已拒绝
     */
    override fun showPermissionBanned() {
        binding.defaultLayout.visibility = View.GONE
        binding.authorizedLayout.visibility = View.GONE
        binding.bannedLayout.visibility = View.VISIBLE

        binding.readTipsLayout.visibility = View.GONE
    }

    /**
     * 进入登录页面
     */
    override fun showLoginPage() {
        onIntent(LoginActivity::class.java)
        finish()
    }

    /**
     * 添加指示器
     */
    private fun addIndicatorViews(index: Int){
        val item = presenter!!.getCashRootPermissionList()[index]
        binding.indicatorLayout.removeAllViews()
        for (i in 0..<pageList!!.size){
            val indicatorBinding = CashPermissionIndicatorBinding.inflate(layoutInflater)
            when (i){
                index -> {
                    indicatorBinding.imgView.setImageResource(R.drawable.cash_permission_indicator_select)
                }
                else -> {
                    indicatorBinding.imgView.setImageResource(R.drawable.cash_permission_indicator_normal)
                }
            }
            binding.indicatorLayout.addView(indicatorBinding.root)
        }
    }


    class MyFragmentPagerAdapter(fragmentActivity: FragmentActivity, val pageList: ArrayList<ProFragment>) : FragmentStateAdapter(fragmentActivity) {
        override fun createFragment(position: Int): Fragment {
            return pageList[position]
        }

        override fun getItemCount(): Int {
            return pageList.size
        }
    }

    override fun onStart() {
        super.onStart()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_PERMISSION_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addCashRootEventPoint(CashRootEventType.PAGE.CASHROOT_PAGE_PERMISSION_END)
    }
}